package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.entity.MessageConstant;
import com.github.pagehelper.Page;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.ExceptionLog;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.PictureSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/pictureSort")
@Api(tags = "图片分类相关接口")
@CrossOrigin
public class PictureSortController {
    @Autowired
    private PictureSortService pictureSortService;

    /**
     * 添加图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加图片分类")
    public String add(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        if (pictureSortVO.getSort() > 100) {
            return ResultUtil.result(StatusCode.ERROR, "设置分类失败");
        }
        try {
            pictureSortService.add(pictureSortVO);
            return ResultUtil.result("success", "添加图片分类成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(StatusCode.ERROR, "添加图片分类失败");
        }
    }

    /**
     * 删除图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除图片分类")
    public String delete(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        if (pictureSortVO.getUid() == null || "".equals(pictureSortVO.getUid())) {
            return ResultUtil.result(401, "uid不能为空");
        }
        try {
            pictureSortService.delete(pictureSortVO);
            return ResultUtil.result("success", "图片分类删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, "图片分类删除失败");
        }
    }

    /**
     * 编辑图片分类
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑图片分类")
    public String edit(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        if (pictureSortVO.getSort() > 100) {
            return ResultUtil.result(StatusCode.ERROR, "设置分类失败");
        }
        if (pictureSortVO.getUid() == null || "".equals(pictureSortVO.getUid())) {
            return ResultUtil.result(401, "Uid不能为空");
        }
        try {
            pictureSortService.edit(pictureSortVO);
            return ResultUtil.result("success", "添加图片分类成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(StatusCode.ERROR, "添加图片分类失败");
        }
    }

    /**
     * 获取图片分类列表
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/getList")
    @ApiOperation("获取图片分类列表")
    public String getList(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        try {
            Page<PictureSort> list = pictureSortService.getList(pictureSortVO);
            Map<String, Object> map = new HashMap();
            map.put("records",list.getResult());
            map.put("total",list.getTotal());
            map.put("size",list.getPageSize());
            map.put("current",list.getPageNum());
            map.put("orders","");
            map.put("optimizeCountSql",true);
            map.put("isSearchCount",true);
            return ResultUtil.result("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_FINDALL_FAIL);
        }
    }

    /**
     * 通过Uid获取图片分类列表
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/getPictureSortByUid")
    @ApiOperation("通过Uid获取图片分类列表")
    public String getPictureSortByUid(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        if (pictureSortVO.getUid() == null || "".equals(pictureSortVO.getUid())) {
            return ResultUtil.result(401, "Uid不能为空");
        }
        try {
            PictureSort pictureSort = pictureSortService.getPictureSortByUid(pictureSortVO);
            return ResultUtil.result("success", pictureSort);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(StatusCode.ERROR, "通过Uid获取图片分类失败");
        }
    }

    /**
     * 图片分类置顶
     *
     * @param pictureSortVO
     * @return
     */
    @PostMapping("/stick")
    @ApiOperation("图片分类置顶")
    public String stick(@RequestBody(required = false) PictureSortVO pictureSortVO) {
        try {
            Page<PictureSort> pictureSorts = pictureSortService.stick(pictureSortVO);
            Map<String, Object> map = new HashMap();
            map.put("records",pictureSorts.getResult());
            map.put("total",pictureSorts.getTotal());
            map.put("size",pictureSorts.getPageSize());
            map.put("current",pictureSorts.getPageNum());
            map.put("orders","");
            map.put("optimizeCountSql",true);
            map.put("isSearchCount",true);
            return ResultUtil.result("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(StatusCode.ERROR, "图片分类失败");
        }
    }
}
